package com.link.base.base.report.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.report.model.PurchaseArriveReport;
import com.link.base.base.report.model.PurchaseReport;
import com.link.base.base.report.model.PurchaseStatusReport;
import com.link.base.base.report.service.PurchaseReportService;
import com.link.base.base.report.service.PurchaseStatusReportService;
import com.link.base.base.system.model.Period;
import com.link.base.base.system.service.PeriodService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Wei Xuan
 * @description: 要货计划汇总、明细报表
 * @created: 2018/12/01 14:35
 * @modified By:
 */
@Controller
@RequestMapping("/link/report")
public class PurchaseReportController extends BasicController<PurchaseReport> {

    @Resource
    private PurchaseReportService purchaseReportService;

    @Resource
    private PeriodService periodService;

    @Resource
    private PurchaseStatusReportService purchaseStatusReportService;

    @RequestMapping(value = "/queryPurchasePlanProduct", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPurchasePlanProduct(@JsonParam PurchaseReport record, @JsonParam QueryParams qps, HttpSession session,
                                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            Long periodId = record.getPeriodId();
            Long accntId = record.getAccntId();
            Long prodId = record.getProdId();
            String platformType = record.getPlatformType();
            PurchaseReport entity = new PurchaseReport();
            if (null == periodId) {
                throw new ServiceException("REPORT-002");
            }
            if (null == accntId) {
                throw new ServiceException("REPORT-003");
            }
            if (StringUtils.isBlank(platformType)) {
                throw new ServiceException("REPORT-004");
            }
            entity = (PurchaseReport) BasicModel.transformClass(entity, qps);
            entity.setAttr3(periodId.toString());
            entity.setAttr4(accntId.toString());
            if (null != prodId) {
                entity.setAttr5(prodId.toString());
            }
            entity.setPlatformType(platformType);
            List<PurchaseReport> purchaseReportList = purchaseReportService.queryPurchasePlanProduct(entity);
            map.put("success", true);
            map.put("rows", purchaseReportList);
            map.put("total", getCount(entity, purchaseReportList.size()));
        } catch (Exception e) {
            map.put("success", false);
            map.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    @RequestMapping(value = "/queryPurchaseStatusList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPurchaseStatusList(@JsonParam PurchaseReport record, @JsonParam QueryParams qps, HttpSession session,
                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            Long periodId = record.getPeriodId();
            Long accntId = record.getAccntId();

            PurchaseStatusReport entity = new PurchaseStatusReport();
            if (null == periodId) {
                throw new ServiceException("REPORT-002");
            }
            entity.setPeriodId(periodId);
            if (null == accntId) {
                CoreUser user = UserUtil.getUser();
                Long vendorId = user.getOrgId();
                if (null == vendorId) {
                    throw new ServiceException("REPORT-005");
                }
                entity.setVendorId(vendorId);
            }
            entity.setAccntId(accntId);
            entity = (PurchaseStatusReport) BasicModel.transformClass(entity, qps);
            List<PurchaseStatusReport> purchaseStatusReportList = purchaseStatusReportService.queryPurchaseStatusList(entity);
            map.put("success", true);
            map.put("rows", purchaseStatusReportList);
            map.put("total", getCount(entity, purchaseStatusReportList.size()));
            map.put("code", "200");
        } catch (Exception e) {
            map.put("success", false);
            map.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    @RequestMapping(value = "/queryPurchaseStatusList/exportData", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPurchaseStatusListExportData(@JsonParam PurchaseReport record, @JsonParam QueryParams qps, HttpSession session,
                                                                 HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("queryPurchaseStatusList/exportData")
                .withMessage("导出列表...").flush();
        Map<String, Object> map = new HashMap<>();
        try {
            Long periodId = record.getPeriodId();
            Long accntId = record.getAccntId();
            ExportWriter exportWriter = ExportFactory.Excel2007Instance(qps);
            PurchaseStatusReport entity = (PurchaseStatusReport) BasicModel.transformClass(new PurchaseStatusReport(), qps);
            if (null == periodId) {
                throw new ServiceException("REPORT-002");
            }
            entity.setPeriodId(periodId);
            if (null == accntId) {
                CoreUser user = UserUtil.getUser();
                Long vendorId = user.getOrgId();
                if (null == vendorId) {
                    throw new ServiceException("REPORT-005");
                }
                entity.setVendorId(vendorId);
            }
            entity.setAccntId(accntId);
            List<PurchaseStatusReport> purchaseStatusReportList = purchaseStatusReportService.queryPurchaseStatusList(entity);
            while (!qps.getStopExport()) {
                exportWriter.wirteData(purchaseStatusReportList);
            }
            map.put("success", true);
            map.put("url", exportWriter.getUrlPath());
        } catch (Exception e) {
            map.put("success", false);
            map.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 查询区间产品数量
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "/queryPeriodProdAmt", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPeriodProdAmt(@JsonParam PurchaseArriveReport record, @JsonParam QueryParams qps, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<PurchaseArriveReport> list = new ArrayList<PurchaseArriveReport>();
            //区域ID
            Long accntId = record.getAccntId();
            //期间ID
            Long periodId = record.getPeriodId();
            //产品ID
            Long prodId = record.getProdId();

            if (null == accntId) {
                throw new ServiceException("REPORT-003");
            }
            if (null == periodId) {
                throw new ServiceException("REPORT-002");
            }

            Period period = periodService.queryById(periodId);
            if (period != null) {
                String periodName = period.getTitle();
                String startDt = period.getStartDate();
                String endDt = period.getEndDate();
                record.setPeriodName(periodName);
                record.setPeriodStartDt(startDt);
                record.setPeriodEndDt(endDt);
            } else {
                throw new ServiceException("REPORT-006");
            }

            if (null == prodId) {
                throw new ServiceException("REPORT-007");
            }
            list = purchaseReportService.queryPeriodProdAmt(record);
            map.put("success", true);
            map.put("rows", list);
        } catch (Exception e) {
            map.put("success", false);
            map.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 导出
     */
    @RequestMapping(value = "/queryPurchasePlanProduct/exportData")
    @ResponseBody
    public Map<String, Object> exportData(PurchaseReport record, QueryParams qps, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("queryPurchasePlanProduct/exportData")
                .withMessage("导出列表...").flush();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Long periodId = record.getPeriodId();
            Long accntId = record.getAccntId();
            Long prodId = record.getProdId();

            PurchaseReport entity = new PurchaseReport();
            if (null == periodId) {
                throw new ServiceException("REPORT-002");
            }
            if (null == accntId) {
                throw new ServiceException("REPORT-003");
            }
            ExportWriter exportWriter = ExportFactory.Excel2007Instance(qps);
            entity = (PurchaseReport) BasicModel.transformClass(record, qps);
            beforQueryAllOrExample(entity, request);
            beforQueryExample(entity, request);
            entity.setAttr3(periodId.toString());
            entity.setAttr4(accntId.toString());
            entity.setAttr5(prodId.toString());
            List<PurchaseReport> list = purchaseReportService.queryPurchasePlanProduct(entity);
            while (!qps.getStopExport()) {
                exportWriter.wirteData(list);
            }
            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
